/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：ShopCompanyServiceImpl.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-11 下午1:45:34
 * 版本号：V1.1
 */
package com.ywt.cda.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;

import javax.annotation.Resource;

import com.ywt.cda.utils.JedisUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.action.model.GoodsModel;
import com.ywt.cda.core.dao.page.Page;
import com.ywt.cda.dao.Ana2ndDataDao;
import com.ywt.cda.dao.Ana3rdDataDao;
import com.ywt.cda.dao.AnaExportDataDao;
import com.ywt.cda.dao.AnaImportDataDao;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.domain.Ana3rdData;
import com.ywt.cda.domain.AnaExportData;
import com.ywt.cda.domain.AnaImportData;
import com.ywt.cda.domain.MCategory;
import com.ywt.cda.domain.SCategory;
import com.ywt.cda.domain.enums.AnaTable;
import com.ywt.cda.domain.enums.DimensionFlag;
import com.ywt.cda.domain.enums.OrderByEnum;
import com.ywt.cda.service.ConfDataCacheService;
import com.ywt.cda.service.GoodsService;
import com.ywt.cda.service.GovAnalysisService;
import com.ywt.cda.service.vo.ExportGoodsDetailVo;
import com.ywt.cda.utils.DateUtils;

/**
 * （一句话功能简述）
 * <br>（功能详细描述）
 * @author lougf
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-11 下午1:45:34
 */
@Service
public class GoodsServiceImpl implements GoodsService {

	@Resource
	private GovAnalysisService govAnalysisService;
	@Resource
	private Ana2ndDataDao ana2ndDataDao;
	@Resource
	private AnaImportDataDao anaImportDataDao;
	@Resource
	private AnaExportDataDao anaExportDataDao;
	@Resource
	private Ana3rdDataDao ana3rdDataDao;
	@Resource
	private ConfDataCacheService confDataCacheService;
	private final String YYYY_MM="yyyy-MM";
	@Override
	public List<Ana2ndData> getTopNGoodsData(GoodsModel model, int topN,
			OrderByEnum orderBy, DimensionFlag dimensionFlag) {
		Page page = new Page(1,topN);
		AnaTable table=AnaTable.TB_ANA_DATA_2ND_GOV_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_2ND_GOV_M;
		}
		List<Ana2ndData> datas = govAnalysisService.queryTotalDataWithPage(
				model.getStartTime_F(), model.getEndTime_F(),
				table, null,
				dimensionFlag.getCode(), orderBy.name(),"t.MAIN_CODE", page);
		return datas;
	}

	@Override
	public List<Ana2ndData> getMcategoryByDateFlag(GoodsModel model){
		List<Ana2ndData> datas = govAnalysisService.getMcategoryByDateFlag(
				model.getStartTime_F(), model.getEndTime_F());
		return datas;
	}

	public List<Ana3rdData> getGoodsMData(int month){
		Date dNow = new Date();
		String startDate;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dNow);//把当前时间赋给日历
		calendar.add(calendar.MONTH, -month);
		startDate = DateUtils.format(calendar.getTime(), "yyyy-MM");
		return govAnalysisService.getGoodsMData(startDate);
	}

	@Override
	public List<Ana3rdData> getTBandHB(String date1, String date2, String date3) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("date1", date1);
		params.put("date2", date2);
		params.put("date3", date3);
		return govAnalysisService.getTBandHB(params);
	}

	@Override
	public List<Map<String, Object>> goodsCategoriesMList(String firstday, String today) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", firstday);
		params.put("today", today);
		return anaExportDataDao.goodsCategoriesMList(params);
	}

	@Override
	public List<Map<String, Integer>> goodsCategoriesByCountryMList(String firstday, String today, String main_code) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", firstday);
		params.put("today", today);
		params.put("main_code", main_code);
		return anaExportDataDao.goodsCategoriesByCountryMList(params);
	}

	@Override
	public List<Map<String, Object>> getTopCountryList(String firstday, String today, int topN) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", firstday);
		params.put("today", today);
		params.put("topN", topN);
		return anaExportDataDao.getTopCountryList(params);
	}

	@Override
	public List<Map<String, Integer>> getTopCountryCategoriesList(String firstday, String today, String countryCode, int topN) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", firstday);
		params.put("today", today);
		params.put("countryCode", countryCode);
		params.put("topN", topN);
		return anaExportDataDao.getTopCountryCategoriesList(params);
	}

	@Override
	public List<String> getTopCategoryList(String firstday, String today, int topN) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", firstday);
		params.put("today", today);
		params.put("topN", topN);
		return anaExportDataDao.getTopCategoryList(params);
	}

	@Override
	public List<Map<String,Object>> getCountryByCategoriesList(String firstday, String today, String countryCode, List<String> topCategoryList) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", firstday);
		params.put("today", today);
		params.put("countryCode", countryCode);
		params.put("topCategoryList", topCategoryList);
		return anaExportDataDao.getCountryByCategoriesList(params);
	}

	@Override
	public List<Integer> getSumCategoryNum(String firstday, String today, List<String> topCategoryList) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", firstday);
		params.put("today", today);
		params.put("topCategoryList", topCategoryList);
		return anaExportDataDao.getSumCategoryNum(params);
	}

	@Override
	public List<Map<String, Object>> getGoodRangeData(int topN) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("topN", topN);
		return anaExportDataDao.getGoodRangeData(params);
	}

    @Override
    public List<Map<String, Object>> getCategoryMonthData(int monthN, String categoryName) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("monthN", monthN);
        params.put("categoryName", categoryName);
        return anaExportDataDao.getCategoryMonthData(params);
    }

	@Override
	public Integer getYesterdayCount(String yesterday) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("yesterday", yesterday);
		return anaExportDataDao.getYesterdayCount(params);
	}

	@Override
	public Integer getThisMonthCount(String thisMonth) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("thisMonth", thisMonth);
		return anaExportDataDao.getThisMonthCount(params);
	}

	@Override
	public Integer getThisYearCount(String thisYear) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("thisYear", thisYear);
		return anaExportDataDao.getThisYearCount(params);
	}

	@Override
	public List<Map<String, Object>> getTopCountryNameList(String firstday, String today, int topN) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", firstday);
		params.put("today", today);
		params.put("topN", topN);
		return anaExportDataDao.getTopCountryNameList(params);
	}

	@Override
	public Integer getAllCountrySum() {
		return anaExportDataDao.getAllCountrySum();
	}

	@Override
	public Map<String, Object> getSumSalesCount() {
		return anaExportDataDao.getSumSalesCount();
	}

	@Override
	public Map<String, Object> getDHSumSalesCount() {
		return anaExportDataDao.getDHSumSalesCount();
	}

	@Override
	public List<Map<String, Object>> getTopCategorySumList(String starttime, String endtime, int i) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("firstday", starttime);
		params.put("today", endtime);
		params.put("topN", i);
		return anaExportDataDao.getTopCategorySumList(params);
	}

	@Override
	public Integer getCoustomerNum() {
		return anaExportDataDao.getCoustomerNum();
	}

	@Override
	public List<Map<String, Object>> getAllCountryCategory(List<String> topCategoryList) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("topCategoryList", topCategoryList);
		return anaExportDataDao.getAllCountryCategory(params);
	}

    @Scheduled(cron="0/10 * * * * ?")
	public void updateContinentTop10Data(){
        JedisUtil jedisUtil = JedisUtil.getInstance();
        if(!jedisUtil.exists("continentTop10")) {
            JSONObject jsonResult = new JSONObject();
            String json;
            List<List<Map<String, String>>> result = new ArrayList<>();
            result.add(anaExportDataDao.getContinentTop10("亚洲"));
            result.add(anaExportDataDao.getContinentTop10("美洲"));
            result.add(anaExportDataDao.getContinentTop10("欧洲"));
            jsonResult.put("data",result);
            json = jsonResult.toString();
            jedisUtil.setnxWithTimeOut("continentTop10", json, 1800); // 有效期半个小时
        }
    }

	@Override
	public String getContinentTop10() {
        JedisUtil jedisUtil = JedisUtil.getInstance();
        JSONObject jsonResult = new JSONObject();
		String json;
		if(jedisUtil.exists("continentTop10_BJ")) {
			json = jedisUtil.get("continentTop10_BJ");
		}else{
            List<List<Map<String, String>>> result = new ArrayList<>();
            result.add(anaExportDataDao.getContinentTop10("亚洲"));
            result.add(anaExportDataDao.getContinentTop10("美洲"));
            result.add(anaExportDataDao.getContinentTop10("欧洲"));
            jsonResult.put("data",result);
            json = jsonResult.toString();
            jedisUtil.del("continentTop10_BJ");
            jedisUtil.setnxWithTimeOut("continentTop10_BJ", json, 1800); // 有效期半个小时
		}

		return json;
	}

	@Override
	public void toJsonArray(List<Ana2ndData> data,JSONArray jsonArray,OrderByEnum orderByEnum, DimensionFlag dimensionFlag) {
		// TODO Auto-generated method stub
		
		if (data != null) {
			BigDecimal sumNum=new BigDecimal(0);
			BigDecimal sumPrice=new BigDecimal(0);
			BigDecimal sumPkg=new BigDecimal(0);
			for (int i = 0; i < data.size(); i++) {
				Ana2ndData ana2ndData = data.get(i);
				sumNum=sumNum.add(ana2ndData.getSumNum());
				sumPrice=sumPrice.add(ana2ndData.getSumPrice_());
				sumPkg=sumPkg.add(ana2ndData.getSumPkg_());
				
			}
			for (int i = 0; i < data.size(); i++) {
				Ana2ndData ana2ndData = data.get(i);
				JSONObject nameValue = new JSONObject();

				String categoryName = null;
				if(dimensionFlag.getCode().equals(DimensionFlag.S_CATEGORY.getCode())){
					SCategory sCategory = confDataCacheService.getsCategoryMap().get(ana2ndData.getMainCode());
					if(null != sCategory){
						categoryName = sCategory.getsCategoryName();
					}else{
						MCategory mCategory = confDataCacheService.getmCategoryMap().get(ana2ndData.getMainCode());
						categoryName = (null == mCategory?ana2ndData.getMainCode():mCategory.getmCategoryName());
					}

				}else{
					categoryName = ana2ndData.getMainCode();
					
				}
				nameValue.put("name", categoryName);

				switch (orderByEnum) {
				case SUM_NUM:
					nameValue.put("value", ana2ndData.getSumNum());
					
					BigDecimal percent=ana2ndData.getSumNum().divide(sumNum,4,BigDecimal.ROUND_HALF_UP);
					percent=percent.multiply(new BigDecimal(100));
					percent=percent.setScale(2, BigDecimal.ROUND_HALF_UP);
					nameValue.put("percent", percent);
					break;
				default:

					break;
				}


				jsonArray.add(nameValue);

			}

		}
	}

	@Override
	public void initDataMainName(List<Ana2ndData> data,
			DimensionFlag dimensionFlag)
	{
		  if(null != data && data.size() > 0){
	            for(Ana2ndData temp : data){
	            	if(dimensionFlag.getCode().equals(DimensionFlag.S_CATEGORY.getCode())){
						SCategory sCategory = confDataCacheService.getsCategoryMap().get(temp.getMainCode());
						if(null != sCategory){
							temp.setMainName(sCategory.getsCategoryName()); 
						}else{
							MCategory mCategory = confDataCacheService.getmCategoryMap().get(temp.getMainCode());
							temp.setMainName(null == mCategory?temp.getMainCode():mCategory.getmCategoryName());
						}

					}else{
						temp.setMainName(temp.getMainCode()); 
						
					}   
	            }
	        }
		
	}

	@Override
	public List<AnaImportData> getTopNGoodsImortData(GoodsModel model, int topN, OrderByEnum orderBy, DimensionFlag dimensionFlag) {
		Page page = new Page(1,topN);
		AnaTable table=AnaTable.TB_ANA_IMPORT_DATA_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_IMPORT_DATA_M;
		}
		List<AnaImportData> datas = govAnalysisService.queryTotalImportDataWithPage(
				model.getStartTime_F(), model.getEndTime_F(),
				table, null,
				dimensionFlag.getCode(), orderBy.name(),"t.DIMENSION_CODE", page);
		return datas;
	}

	@Override
	public void toJsonArrayImort(List<AnaImportData> data, JSONArray jsonArray, OrderByEnum orderByEnum, DimensionFlag dimensionFlag) {

	}

	@Override
	public void initImportDataMainName(List<AnaImportData> data,
								 DimensionFlag dimensionFlag)
	{
		if(null != data && data.size() > 0){
			for(AnaImportData temp : data){
				temp.setMainCode(temp.getDimensionCode());
				temp.setMainName(temp.getDimensionName());
				if(null == temp.getMainCode()){
					temp.setMainCode(temp.getSecondCode());

				}
				if(dimensionFlag.getCode().equals(DimensionFlag.S_CATEGORY.getCode())){
					SCategory sCategory = confDataCacheService.getsCategoryMap().get(temp.getMainCode());
					if(null != sCategory){
						temp.setMainName(sCategory.getsCategoryName());
					}else{
						MCategory mCategory = confDataCacheService.getmCategoryMap().get(temp.getMainCode());
						temp.setMainName(null == mCategory?temp.getMainCode():mCategory.getmCategoryName());
					}

				}else{
					temp.setMainName(temp.getMainCode());

				}
			}
		}

	}

	@Override
	public List<String> getItemNameListByKeyword(String keyword) {
		AnaTable table=AnaTable.TB_ANA_DATA_IMPORT_3RD_M;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("secondFlag", DimensionFlag.M_CATEGORY.getCode());
		params.put("likeItemName", keyword);
		params.put("limit", 10);

		return anaImportDataDao.getItemNameListByKeyword(params);
	}


	@Override
	public List<AnaImportData> getGoodsImortDataByDateFlag(GoodsModel model,String province,String keywordCode) {
		AnaTable table=AnaTable.TB_ANA_DATA_IMPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_IMPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("groupCode", "DATE_FLAG");
		params.put("orderBy", "DATE_FLAG");
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("secondFlag", DimensionFlag.M_CATEGORY.getCode());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		if(!StringUtils.isEmpty(province)){
			params.put("code", province);
		}
		return anaImportDataDao.listTotalDataByParamsAndGoods(params);
	}

	@Override
	public List<AnaImportData> getGoodsImortDataByCountry(GoodsModel model,String province,String keywordCode) {
		AnaTable table=AnaTable.TB_ANA_DATA_IMPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_IMPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("groupCode", "SECOND_CODE");
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("orderBy", OrderByEnum.SUM_NUM.name());
		params.put("secondFlag", DimensionFlag.EXPORT_COUNTRY.getCode());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		if(!StringUtils.isEmpty(province)){
			params.put("code", province);
		}
		return anaImportDataDao.listTotalDataByParamsAndGoods(params);

	}

	@Override
	public List<AnaImportData> getGoodsImortDataByCategory(GoodsModel model, String province, String keywordCode) {
		AnaTable table=AnaTable.TB_ANA_DATA_IMPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_IMPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("groupCode", "SECOND_CODE");
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("orderBy", OrderByEnum.SUM_NUM.name());
		params.put("secondFlag", DimensionFlag.M_CATEGORY.getCode());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		if(!StringUtils.isEmpty(province)){
			params.put("code", province);
		}
		return anaImportDataDao.listTotalDataByParamsAndGoods(params);
	}

	@Override
	public Long getAllProvinceSumNum(GoodsModel model,  String keywordCode){
		AnaTable table=AnaTable.TB_ANA_DATA_IMPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_IMPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		params.put("secondFlag", DimensionFlag.EXPORT_COUNTRY.getCode());
		return anaImportDataDao.getAllProvinceSumNum(params);

	}

	@Override
	public List<AnaImportData> getGoodsImortDataBySex(GoodsModel model, String province, String keywordCode) {
		AnaTable table=AnaTable.TB_ANA_DATA_IMPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_IMPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("groupCode", "SECOND_CODE");
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("orderBy", OrderByEnum.SUM_NUM.name());
		params.put("secondFlag", DimensionFlag.SEX_TYPE.getCode());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		if(!StringUtils.isEmpty(province)){
			params.put("code", province);
		}
		return anaImportDataDao.listTotalDataByParamsAndGoods(params);
	}

	@Override
	public List<AnaImportData> getGoodsImortDataByAge(GoodsModel model, String province, String keywordCode) {
		AnaTable table=AnaTable.TB_ANA_DATA_IMPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_IMPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("groupCode", "substr(SECOND_CODE,0,4)");
		params.put("asName", " as SECOND_CODE");
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("orderBy", OrderByEnum.SUM_NUM.name());
		params.put("secondFlag", DimensionFlag.AGE_TYPE.getCode());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		if(!StringUtils.isEmpty(province)){
			params.put("code", province);
		}
		return anaImportDataDao.listTotalDataByParamsAndGoods(params);
	}

	@Override
	public List<String> getExportItemNameListByKeyword(String keyword) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("mainFlag", DimensionFlag.GOODS_TYPE.getCode());
		params.put("likeItemName", keyword);
		params.put("limit", 15);

		return ana2ndDataDao.getItemNameListByKeyword(params);
	}

	@Override
	public List<AnaExportData> getGoodsExportDataByDateFlag(GoodsModel model, String country, String keywordCode) {
		AnaTable table=AnaTable.TB_ANA_DATA_EXPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_EXPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("groupCode", "DATE_FLAG");
		params.put("orderBy", "DATE_FLAG");
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("secondFlag", DimensionFlag.EXPORT_COUNTRY.getCode());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		if(!StringUtils.isEmpty(country)){
			params.put("secondCode", country);
		}
		return anaExportDataDao.listTotalDataByParamsAndGoods(params);
	}

	@Override
	public List<AnaExportData> getGoodsExportDataByCountry(GoodsModel model, String keywordCode) {
		AnaTable table=AnaTable.TB_ANA_DATA_EXPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_EXPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("groupCode", "SECOND_CODE");
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("orderBy", OrderByEnum.SUM_NUM.name());
		params.put("secondFlag", DimensionFlag.EXPORT_COUNTRY.getCode());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		return anaExportDataDao.listTotalDataByParamsAndGoods(params);
	}

	@Override
	public Long getAllExportCountrySumNum(GoodsModel model, String keywordCode) {
		AnaTable table=AnaTable.TB_ANA_DATA_EXPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_EXPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		if(!StringUtils.isEmpty(keywordCode)){
			params.put("mainCode", keywordCode);
		}
		params.put("secondFlag", DimensionFlag.EXPORT_COUNTRY.getCode());
		return anaExportDataDao.getAllExportCountrySumNum(params);

	}

	@Override
	public ExportGoodsDetailVo getTopOneData(GoodsModel exportGoodsModel,
			List<Ana2ndData> datas3) {
		//先查page
		if(null==datas3||datas3.size()==0){
			return null;
		}
		ExportGoodsDetailVo toReturn=new ExportGoodsDetailVo();
		//取第一个二维数据
		Ana2ndData topOne=datas3.get(0);
		String mainCode=topOne.getMainCode();
		AnaTable table=AnaTable.TB_ANA_DATA_EXPORT_3RD_D;
		if(YYYY_MM.equals(exportGoodsModel.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_EXPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("endDate", exportGoodsModel.getEndTime_F());
		params.put("startDate", exportGoodsModel.getStartTime_F());
		params.put("secondCode", mainCode);
		params.put("mainFlag", DimensionFlag.GOODS_TYPE.getCode());
		params.put("secondFlag", DimensionFlag.EXPORT_COUNTRY.getCode());
		Page page=new Page(1,5);
		List<Ana3rdData> goodsList=ana3rdDataDao.listByParam(params, page);
		page.setResult(goodsList);
		toReturn.setPage(page);
		Ana3rdData firstGood=null;
		if(null!=goodsList&&goodsList.size()>0){
			firstGood=goodsList.get(0);
		}else{
			return toReturn;
		}
		params.put("mainCode", firstGood.getMainCode());
		List<Ana3rdData> firstGoodsList=ana3rdDataDao.listGoodsByParam(params, null);
		toReturn.setChartDatas(firstGoodsList);
		return toReturn;
	}
	@Override
	public ExportGoodsDetailVo getTopOneData(GoodsModel exportGoodsModel,
			String mainCode,Page page) {
		
		ExportGoodsDetailVo toReturn=new ExportGoodsDetailVo();
		
		
		AnaTable table=AnaTable.TB_ANA_DATA_EXPORT_3RD_D;
		if(YYYY_MM.equals(exportGoodsModel.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_EXPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("endDate", exportGoodsModel.getEndTime_F());
		params.put("startDate", exportGoodsModel.getStartTime_F());
		params.put("secondCode", mainCode);
		params.put("mainFlag", DimensionFlag.GOODS_TYPE.getCode());
		params.put("secondFlag", DimensionFlag.EXPORT_COUNTRY.getCode());
		
		List<Ana3rdData> goodsList=ana3rdDataDao.listByParam(params, page);
		page.setResult(goodsList);
		toReturn.setPage(page);
		Ana3rdData firstGood=null;
		if(null!=goodsList&&goodsList.size()>0){
			firstGood=goodsList.get(0);
		}else{
			return toReturn;
		}
		params.put("mainCode", firstGood.getMainCode());
		List<Ana3rdData> firstGoodsList=ana3rdDataDao.listGoodsByParam(params, null);
		toReturn.setChartDatas(firstGoodsList);
		return toReturn;
	}
	@Override
	public ExportGoodsDetailVo getTopOneData(GoodsModel exportGoodsModel,
			String mainCode,String goodsName,Page page) {
		
		ExportGoodsDetailVo toReturn=new ExportGoodsDetailVo();
		
		
		AnaTable table=AnaTable.TB_ANA_DATA_EXPORT_3RD_D;
		if(YYYY_MM.equals(exportGoodsModel.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_EXPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("endDate", exportGoodsModel.getEndTime_F());
		params.put("startDate", exportGoodsModel.getStartTime_F());
		params.put("secondCode", mainCode);
		params.put("mainFlag", DimensionFlag.GOODS_TYPE.getCode());
		params.put("secondFlag", DimensionFlag.EXPORT_COUNTRY.getCode());
		
		List<Ana3rdData> goodsList=ana3rdDataDao.listByParam(params, page);
		page.setResult(goodsList);
		toReturn.setPage(page);
		if(org.apache.commons.lang3.StringUtils.isBlank(goodsName)){
			Ana3rdData firstGood=null;
			if(null!=goodsList&&goodsList.size()>0){
				firstGood=goodsList.get(0);
				params.put("mainCode", firstGood.getMainCode());
			}else{
				params.put("mainCode", "");
			}
			
		}else{
			params.put("mainCode", goodsName);
		}
		
		
		List<Ana3rdData> firstGoodsList=ana3rdDataDao.listGoodsByParam(params, null);
		toReturn.setChartDatas(firstGoodsList);
		return toReturn;
	}
	@Override
	public void setGoodsJsonObject(GoodsModel exportGoodsModel,
			ExportGoodsDetailVo goodsDetailVo, JSONObject goodsJsonObject) {
		List<Ana3rdData> goodsList=goodsDetailVo.getChartDatas();
		if(null==goodsList){
			return ;
		}
		Map<String, Ana3rdData> groupMap=new TreeMap<String, Ana3rdData>();
		for(Ana3rdData data:goodsList){
			groupMap.put(data.getDateFlag(), data);
			goodsJsonObject.put("legend", data.getMainCode());
		}
		//日期list组装开始
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Date today = new Date();

		String specifiedMonth = simpleDateFormat.format(today);
		List<String> dateList = null;
		if (null != exportGoodsModel.getTimeSection()) {
			switch (exportGoodsModel.getTimeSection()) {

			case SIXMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 5),
						specifiedMonth);

				break;
			case NINEMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 8),
						specifiedMonth);

				break;
			case TWELVEMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 11),
						specifiedMonth);

				break;
			default:
				dateList = DateUtils.getDateList(
						exportGoodsModel.getStartTime_F(),
						exportGoodsModel.getEndTime_F());
				break;
			}
		}
		else {
			dateList = DateUtils.getDateList(exportGoodsModel.getStartTime_F(),
					exportGoodsModel.getEndTime_F());
		}
		//日期list组装结束
		//数据补齐
		for(String date:dateList){
			if(!groupMap.containsKey(date)){
				Ana3rdData temp=new Ana3rdData();
				temp.setSumNum(new BigDecimal(0));
				temp.setAvgPrice(new BigDecimal(0));
				groupMap.put(date, temp);
			}
		}
		Iterator<Entry<String, Ana3rdData>> iterator=groupMap.entrySet().iterator();
		JSONArray dateArray=new JSONArray();
		JSONArray sumArray=new JSONArray();
		JSONArray avgArray=new JSONArray();
		while(iterator.hasNext()){
			Entry<String, Ana3rdData> data=iterator.next();
			String date=data.getKey();
			Ana3rdData value=data.getValue();
			dateArray.add(date);
			sumArray.add(value.getSumNum());
			BigDecimal temp =value.getAvgPrice();
			temp = temp.setScale(4, BigDecimal.ROUND_HALF_UP);
			avgArray.add(temp);
			
			
		}
		goodsJsonObject.put("dateList", dateArray);
		goodsJsonObject.put("sumArray", sumArray);
		goodsJsonObject.put("avgArray", avgArray);
		
	}

	@Override
	public void updateModel(GoodsModel exportGoodsModel) {
		// TODO Auto-generated method stub
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Date today = new Date();

		String specifiedMonth = simpleDateFormat.format(today);
		if (null != exportGoodsModel.getTimeSection()) {
			switch (exportGoodsModel.getTimeSection()) {
			case SENVENDAY:

				break;
			case FIFTEENDAY:

				break;
			case THIRTYDAY:

				break;
			case SIXMONTH:

				exportGoodsModel.setStartTime_F(DateUtils
						.getSpecifiedMonthBefore(specifiedMonth, 5));
				exportGoodsModel.setEndTime_F(specifiedMonth);
				exportGoodsModel.setStartTimePattern(YYYY_MM);

				break;
			case NINEMONTH:

				exportGoodsModel.setStartTime_F(DateUtils
						.getSpecifiedMonthBefore(specifiedMonth, 8));
				exportGoodsModel.setEndTime_F(specifiedMonth);
				exportGoodsModel.setStartTimePattern(YYYY_MM);

				break;
			case TWELVEMONTH:
				exportGoodsModel.setStartTime_F(DateUtils
						.getSpecifiedMonthBefore(specifiedMonth, 11));
				exportGoodsModel.setEndTime_F(specifiedMonth);
				exportGoodsModel.setStartTimePattern(YYYY_MM);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public List<AnaImportData> getImportDataByCode(GoodsModel model,String mainCode,String secondFlag)
	{
		AnaTable table=AnaTable.TB_ANA_IMPORT_3RD_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_IMPORT_3RD_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", table.getTableName());
		params.put("groupCode", "SECOND_CODE");
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("orderBy", OrderByEnum.SUM_NUM.name());
		params.put("secondFlag",secondFlag);
		if(!StringUtils.isEmpty(mainCode)){
			String[] codeArr = mainCode.split(",");
			params.put("mainCodeList",codeArr);
		}
		
		return anaImportDataDao.listTotalDataByParamsAndGoods(params);
	}

}
